package com.example.forum.services.impl;

import com.example.forum.common.AppResult;
import com.example.forum.common.ResultCode;
import com.example.forum.dao.ArticleReplyMapper;
import com.example.forum.exception.ApplicationException;
import com.example.forum.model.Article;
import com.example.forum.model.ArticleReply;
import com.example.forum.services.IArticleReplyService;
import com.example.forum.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class ArticleReplyServiceImpl implements IArticleReplyService {

    @Resource
    private ArticleReplyMapper articleReplyMapper;
    @Resource
    private ArticleServiceImpl articleService;

    /**
     * 创建一条回复
     * @param articleReply
     */
    @Override
    public void create(ArticleReply articleReply) {
        if (articleReply == null || articleReply.getArticleId() == null ||
                StringUtil.isEmpty(articleReply.getContent())) {
            // 判断评论是否存在
            log.warn(ResultCode.FAILED.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }

        // 当前文章
        Article article =  articleService.selectByPrimaryKey(articleReply.getArticleId());
        if (article == null || article.getState() == 1 || article.getDeleteState() == 1) {
            // 文章不存在
            log.warn(ResultCode.FAILED.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
        articleReply.setReplyId(null);
        articleReply.setReplyUserId(null);
        articleReply.setLikeCount(0);
        articleReply.setState((byte)0);
        articleReply.setDeleteState((byte)0);
        Date date = new Date();
        articleReply.setCreateTime(date);
        articleReply.setUpdateTime(date);
        int row = articleReplyMapper.insertSelective(articleReply);
        log.info("实际影响行数 : " + row);
        if (row != 1) {
            log.warn(ResultCode.FAILED.toString() + " 实际受影响行数 : " + row);
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }

        // 文章评论数量数量加1
        articleService.addOneReplyCountById(articleReply.getArticleId());
        log.info("帖子回复成功");


    }

    @Override
    public List<ArticleReply> selectByArticleId(Long articleId) {
        if (articleId == null || articleId <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Article article = articleService.selectById(articleId);
        if (article == null || article.getDeleteState() == 1) {
            // 文章不存在
            log.warn(ResultCode.FAILED.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
        if (article.getState() == 1) {
            // 文章状态异常
            log.warn(ResultCode.FAILED_NOT_EXISTS.toString() + "文章状态异常");
            throw new ApplicationException(AppResult.failed("文章状态异常"));
        }
        List<ArticleReply> result = articleReplyMapper.selectByArticleId(articleId);
        return result;
    }
}
